﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TelerikAcademyOopProject
{
    public class StockManager
    {

        /*==========================================*/
        //          StockManager FIELDS
        /*==========================================*/
				
        private Dictionary<string, Item> itemsList = new Dictionary<string, Item>();        
        private DatabaseManager db                 = new DatabaseManager();

        /*==========================================*/
        //         StockManager PROPERTIES
        /*==========================================*/

        public Dictionary<string, Item> ItemDic
        {
            get { return itemsList; }
            set { itemsList = value; }
        }

        /*==========================================*/
        //         StockManager METHODS
        /*==========================================*/


        public void AddItem(string ItemType, string itemId, string ItemName, Spred itemPrice, int itemCount)
        {
            // create new class instance of given type
            Item set = ItemFactory.setNewItem(ItemType);
            
            // set some properties
            set.setItemPropertys(itemId, ItemName, itemPrice, itemCount);
            
            // add this item to Item list
            this.itemsList.Add(set.ItemId, set);

            // eventualy write this Item in the Database file
            db.writeFile(set.ItemName);
        }

        public void RemoveItem(string id)
        {
            this.itemsList.Remove(id);
        }


        /*==========================================*/
        //              UI METHODS
        /*==========================================*/
				

        public void setNewItem(){

            Console.Write("Item Type: ");
            string ItemType   =  Console.ReadLine();
            Console.Write("Item ID: ");
            string itemId     =  Console.ReadLine();
            Console.Write("Item Name: ");
            string itemName   =  Console.ReadLine();
            Console.Write("Item Price: ");
            decimal itemPrice =  decimal.Parse(Console.ReadLine());
            Console.Write("Item Count: ");
            int count         =  int.Parse(Console.ReadLine());

            this.AddItem(ItemType, itemId, itemName, new Spred(itemPrice, itemPrice), count);
        }
             

        public void showItems()
        {

            if (this.itemsList.Count > 0)
            {
                Console.WriteLine("|---------------------------------------------------------|");
                Console.WriteLine("|----------------|-Telerik STock Market-|-----------------|");
                Console.WriteLine("|---------------------------------------------------------|");

                foreach (var item in this.itemsList)
                {
                    Console.WriteLine(item.Value);
                }
            }
            else
            {
                Console.WriteLine("|----------------------------------------------------------|");
                Console.WriteLine("|:( Sorry The stock market is closed, No Items are avalabel|");
                Console.WriteLine("|----------------------------------------------------------|");
            }
        }

        public void showCurrency()
        {
            if (this.itemsList.Count > 0)
            {

                Console.WriteLine("|---------------------------------------------------------|");
                Console.WriteLine("|-------------------|-CURRENCY MARKET-|-------------------|");
                Console.WriteLine("|---------------------------------------------------------|");

                foreach (var item in this.itemsList)
                {
                    if (item is Currency)
                    {
                        Console.WriteLine(item.Value);
                    }
                }
            }
            else
            {
                Console.WriteLine("|----------------------------------------------------------|");
                Console.WriteLine("|:( Sorry The stock market is closed, No Items are avalabel|");
                Console.WriteLine("|----------------------------------------------------------|");
            }
        }

        public void showStocks()
        {
            if (this.itemsList.Count > 0)
            {
                Console.WriteLine("|---------------------------------------------------------|");
                Console.WriteLine("|---------------------|-STOCK MARKET-|--------------------|");
                Console.WriteLine("|---------------------------------------------------------|");

                foreach (var item in this.itemsList)
                {
                    if (item is Stock)
                    {
                        Console.WriteLine(item.Value);
                    }
                }
            }
            else
            {
                Console.WriteLine("|----------------------------------------------------------|");
                Console.WriteLine("|:( Sorry The stock market is closed, No Items are avalabel|");
                Console.WriteLine("|----------------------------------------------------------|");
            }
        }


        public void showGoods()
        {
            if (this.itemsList.Count > 0)
            {
                Console.WriteLine("|---------------------------------------------------------|");
                Console.WriteLine("|---------------------|-GOODS MARKET-|--------------------|");
                Console.WriteLine("|---------------------------------------------------------|");

                foreach (var item in this.itemsList)
                {
                    if (item is Goods)
                    {
                        Console.WriteLine(item.Value);
                    }
                }
            }
            else
            {
                Console.WriteLine("|----------------------------------------------------------|");
                Console.WriteLine("|:( Sorry The stock market is closed, No Items are avalabel|");
                Console.WriteLine("|----------------------------------------------------------|");
            }
        }
    }
}
